package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.vo.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 分类管理实体类
 *
 * @Author mr.zhang
 * Date 2025/5/18 20:47
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    /**
     * 新增分类
     *
     * @param categoryDTO
     */
    @Override
    public void save(CategoryDTO categoryDTO) {
        //创建实体类对象
        Category category = new Category();
        //拷贝
        BeanUtils.copyProperties(categoryDTO,category);
        //设置缺失字段
        //分类设置为状态0
        category.setStatus(StatusConstant.DISABLE);
        //设置创建时间、修改时间、创建人、修改人
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(100L);
        category.setUpdateUser(100L);
        categoryMapper.insert(category);
    }

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult pageList(CategoryPageQueryDTO dto) {
        //设置分页参数
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        //调用mapper层方法
        List<Category> categoryList = categoryMapper.pageList(dto);

        //强转为page类型
        Page<Category> page = (Page<Category>)categoryList;

        //返回数据
        return new PageResult(page.getTotal(),page.getResult());
    }


    /**
     * 删除分类
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        //查询是否关联菜品
        //查询是否关联套餐
        categoryMapper.deleteById(id);
    }


    /**
     * 修改分类
     *
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        //设置创建时间与创建人
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @Override
    public List<CategoryVo> listByType(Integer type) {
        List<Category> categoryList = categoryMapper.listByType(type);
        List<CategoryVo> categoryVoList = null;
        categoryVoList = categoryList.stream().map(x->{
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(x,categoryVo);
            categoryVo.setUpdateTime(LocalDateTime.now());
            categoryVo.setUpdateUser(BaseContext.getCurrentId());
            return categoryVo;
        }).collect(Collectors.toList());
        return categoryVoList;
    }

    @Override
    public List<Category> list(Integer type) {
        return categoryMapper.list(type);
    }
}
